package com.app.micaihu.custom.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.bean.speak.IAttUser;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.listener.HandleListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.utils.StatServiceUtils;

import java.util.ArrayList;

/**
 * Created by Guo on 2017/1/17.
 * <p>
 * 关注 的自定义view
 */

public class AttentionView extends FrameLayout implements View.OnClickListener {

    private boolean isSolid;
    private IAttUser iAttUser;
    private TextView text;
    private ProgressBar loading;
    private ImageView icon;
    private LinearLayout background;
    private boolean isUserHome;
    private long startTime;
    private long endTime;
    private long delayedTime;
    private SucessListener sucessListener;

    public AttentionView(Context context) {
        this(context, null);
    }

    public AttentionView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AttentionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context, AttributeSet attrs) {

        inflate(context, R.layout.view_attention, this);
        text = (TextView) findViewById(R.id.text);
        loading = (ProgressBar) findViewById(R.id.load);
        icon = (ImageView) findViewById(R.id.img);
        background = (LinearLayout) findViewById(R.id.background);

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AttentionView);
        if (typedArray != null) {
            //背景和字体是否带有选择器
            isSolid = typedArray.getBoolean(R.styleable.AttentionView_isSolid, false);
            isUserHome = typedArray.getBoolean(R.styleable.AttentionView_isUserHome, false);
            typedArray.recycle();
        }


        //设置字体大小
//        setTextSize(getResources().getDimension(R.dimen.common_font_size11));
//        setTextSize(11);
        text.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.common_font_size11));


        if (isSolid) {
            background.setBackgroundResource(R.drawable.attention_solid);
            text.setTextColor(getResources().getColorStateList(R.color.white_to_gay_enable));
        } else {
            if (!isUserHome) {
                background.setBackgroundResource(R.drawable.attention_stroke);
                text.setTextColor(getResources().getColorStateList(R.color.gray_to_green_enable));
            } else {
                background.setBackgroundResource(R.drawable.common_text_green_round_bg);
                text.setTextColor(getResources().getColor(R.color.common_font_color_6));
            }

        }

    }


    @Override
    public void setTag(Object tag) {
        super.setTag(tag);
        if (tag != null && tag instanceof IAttUser) {
            this.iAttUser = (IAttUser) tag;
            this.setOnClickListener(this);
        }
        if (this.iAttUser != null) {
            flushStatus(this.iAttUser.getIAttType());
        }
    }

    public interface SucessListener {
        void changeView(IAttUser iAttUser);
    }


    public void setSucessListener(SucessListener sucessListener) {
        this.sucessListener = sucessListener;
    }


    private String eventValue;

    public void setEventValue(String eventValue) {
        this.eventValue = eventValue;
    }


    /**
     * 刷新按钮状态
     *
     * @param status
     */
    public void flushStatus(String status) {
        if (TextUtils.isEmpty(status)) {
            status = "0";
        }
        this.iAttUser.setIAttType(status);

        if (UserInforManager.getInstance().isLogin()
                && TextUtils.equals(this.iAttUser.getIToUid(), UserInforManager.getInstance().getUserInfor().getUid())) {
            this.setVisibility(View.GONE);
            return;
        }
        this.setVisibility(View.VISIBLE);

        if (isSolid) {//实心的background
            switch (status) {
                case TypeConstant.ATTENTION_NO:
                    this.icon.getDrawable().setLevel(3);
                    this.background.setEnabled(true);
                    this.text.setEnabled(true);
                    this.text.setText(getResources().getString(R.string.homepage_attention));
                    break;
                case TypeConstant.ATTENTION_YES:
                    this.icon.getDrawable().setLevel(5);
                    this.background.setEnabled(false);
                    this.text.setEnabled(false);
                    this.text.setText(getResources().getString(R.string.homepage_attentioned));
                    break;
                case TypeConstant.ATTENTION_ALL:
                    this.icon.getDrawable().setLevel(1);
                    this.background.setEnabled(false);
                    this.text.setEnabled(false);
                    this.text.setText(getResources().getString(R.string.homepage_attention_eachother));
                    break;
                default:
                    this.setVisibility(View.INVISIBLE);
                    break;
            }


        } else {//边框的background
            if (isUserHome) {//如果为个人主页
                switch (status) {
                    case TypeConstant.ATTENTION_NO:
                        this.icon.getDrawable().setLevel(3);
                        this.text.setText(getResources().getString(R.string.homepage_attention));
                        break;
                    case TypeConstant.ATTENTION_YES:
                        this.icon.getDrawable().setLevel(6);
                        this.text.setText(getResources().getString(R.string.homepage_attentioned));
                        break;
                    case TypeConstant.ATTENTION_ALL:
                        this.icon.getDrawable().setLevel(2);
                        this.text.setText(getResources().getString(R.string.homepage_attention_eachother));
                        break;
                    default:
                        this.setVisibility(View.INVISIBLE);
                        break;
                }
            } else {
                //不是个人主页
                switch (status) {
                    case TypeConstant.ATTENTION_NO:
                        this.icon.getDrawable().setLevel(4);
                        this.background.setEnabled(true);
                        this.text.setEnabled(true);
                        this.text.setText(getResources().getString(R.string.homepage_attention));
                        break;
                    case TypeConstant.ATTENTION_YES:
                        this.icon.getDrawable().setLevel(5);
                        this.background.setEnabled(false);
                        this.text.setEnabled(false);
                        this.text.setText(getResources().getString(R.string.homepage_attentioned));
                        break;
                    case TypeConstant.ATTENTION_ALL:
                        this.icon.getDrawable().setLevel(1);
                        this.background.setEnabled(false);
                        this.text.setEnabled(false);
                        this.text.setText(getResources().getString(R.string.homepage_attention_eachother));
                        break;
                    default:
                        this.setVisibility(View.INVISIBLE);
                        break;
                }
            }

        }

    }

    @Override
    public void onClick(View v) {
        StatServiceUtils.event(Config.EventBauduId.teamlist_attention, "军团_帖子列表关注");
        MobclickAgentUtils.event(UmEventId.attentionbutton_click, eventValue);
        if (UserInforManager.getInstance().isLogin()) {
            InteractiveUtils.getInstance().followUserOperat((AttentionView) v, handleListener);
        } else {
            UserInforManager.getInstance().toLoginPage((Activity) v.getContext());
        }
    }


    /**
     * 点击关注的回调接口
     */
    private HandleListener handleListener = new HandleListener() {

        @Override
        public void handleStart(ArrayList<Object> parms) {
            if (icon != null && loading != null) {
                startTime = System.currentTimeMillis();
                icon.setVisibility(View.GONE);
                loading.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void handleFail(ArrayList<Object> parms) {
            if (icon != null && loading != null) {
                endTime = System.currentTimeMillis();
                if (endTime - startTime < 500) {
                    delayedTime = 500 - (endTime - startTime);
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ((Activity) getContext()).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    icon.setVisibility(View.VISIBLE);
                                    loading.setVisibility(View.GONE);
                                }
                            });
                        }
                    }, delayedTime);
                } else {
                    icon.setVisibility(View.VISIBLE);
                    loading.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public void handleSuccess(ArrayList<Object> parms) {
            if (icon != null && loading != null) {
                if (parms != null) {
                    final String o = (String) parms.get(0);
                    endTime = System.currentTimeMillis();
                    if (endTime - startTime < 500) {
                        delayedTime = 500 - (endTime - startTime);
                        postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ((Activity) getContext()).runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        flushStatus(o);
                                        icon.setVisibility(View.VISIBLE);
                                        loading.setVisibility(View.GONE);
                                    }
                                });
                            }
                        }, delayedTime);
                    } else {
                        flushStatus(o);
                        icon.setVisibility(View.VISIBLE);
                        loading.setVisibility(View.GONE);
                    }
                }


            }
            if (sucessListener != null) {
                sucessListener.changeView(iAttUser);
            }
        }

        @Override
        public void changeView(ArrayList<Object> parms) {

        }
    };
}
